package IO.objectstream;

import java.io.*;

/**
 * 对象流
 * @author 15594
 */
public class ObjectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /**
         *1、序列化过程：将内存中的java对象保存到磁盘中或者进行网络传输
         * */

        //将对象存入文件
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));

        oos.writeObject(new String("你好？"));
        oos.writeObject(new Person(1,"lihua",18,new Account(1,8888.1),666));
        oos.flush();
        oos.close();

        //读取文件中的对象,注意读取顺序必去与写入顺序一致
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
        String s = (String) ois.readObject();
        Person person = (Person) ois.readObject();
        System.out.println(s);
        System.out.println(person);
        ois.close();
    }
}
